$(document).ready(function () {


    $("#chatform").submit(function () {
        document.getElementById('msgcount').innerHTML = document.getElementById('msgcount').innerHTML + 1;

        var jetzt = new Date();

        var clientmsg = $("#usermsg").val();

        $('#chatbox').append('<div class="msg"><div class="chatpic_div"><img class="chatpic" src="http://cdn1.cdnthis.com/chatlp/images/chat_self.gif" /></div> <div class="chatboxcontent"><a href=\'javascript:void(0)\'><b>Gast_245</b></a><br>' + clientmsg + '</div> <div style="clear:both;"></div></div>');


        $("#usermsg").attr("value", "");
        var objDiv = document.getElementById("bb");
        objDiv.scrollTop = objDiv.scrollHeight;

        return false;

    });
});


var offer_url = "http://www.youtube.com/watch?v=WfOez0k_H2Y";
var offer_url2 = "http://www.facebook.com/profile.php?id=100000589376337&ref=tn_tnmn"


var typing_text = "<div id='typingbox'><img src='http://cdn1.cdnthis.com/chatlp/images/fb_bubble_typing.jpg'> Kevin Lay schreibt etwas ...</div>";


var chatbox_preprefix = '<div class="msg"><div class="chatpic_div"><img alt="" class="chatpic" src="';
var chatbox_prefixsuffix = '" /></div> <div class="chatboxcontent"><a href=\'javascript:void(0)\'><b>Kevin Lay</b></a><br/>';

var chatbox_prefix = chatbox_preprefix + 'http://profile.ak.fbcdn.net/hprofile-ak-ash2/27434_100000589376337_2485_q.jpg' + chatbox_prefixsuffix;

var chatbox_suffix = '</div> <div style="clear:both;"></div></div>';


function nexct() {
    url = offer_url;
    window.location = url;
}

function nexct2() {
    url = offer_url2;
    window.location = url;

}

function writing(next) {
    var f = document.getElementById('msgsound');
    f.src = f.src + '&autoplay=1';

    document.getElementById('typing').innerHTML = typing_text;
    setTimeout('window["' + next + '"]()', 2500);
}

function waitformsg(desired, next) {
    if (document.getElementById('msgcount').innerHTML.length >= desired) {
        setTimeout("writing('" + next + "')", randomFromTo(2000, 5000));
    }
    else {
        setTimeout('waitformsg(' + desired + ', "' + next + '")', 100);
    }
}

function randomFromTo(from, to) {
    return Math.floor(Math.random() * (to - from + 1) + from);
}


function init() {

    document.getElementById('usermsg').focus();
    setTimeout("writing('second')", randomFromTo(500, 1000));
}

function second() {

    document.getElementById('typing').innerHTML = "";
    document.getElementById('msgcount').innerHTML = "";
    var jetzt = new Date();

    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'heii <img src="http://cdn1.cdnthis.com/chatlp/images/facebook-smiley-wink.png" />' + chatbox_suffix;

    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    setTimeout("waitformsg(1, 'third')", randomFromTo(2000, 5000));
}

function third() {
    document.getElementById('typing').innerHTML = "";

    var jetzt = new Date();
    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'kommst du auch aus ' + geoip_city().toLowerCase() + '?' + chatbox_suffix;

    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    document.getElementById('msgcount').innerHTML = "";
    setTimeout("waitformsg(1, 'forth')", randomFromTo(2000, 5000));
}

function forth() {
    document.getElementById('typing').innerHTML = "";
    var jetzt = new Date();
    document.getElementById('chatbox').innerHTML += chatbox_prefix + '<img src="http://cdn1.cdnthis.com/chatlp/images/facebook-smiley-grin.png" />D' + chatbox_suffix;
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    setTimeout("writing('fifth')", randomFromTo(2000, 3000));
}

function fifth() {
    document.getElementById('typing').innerHTML = "";

    var jetzt = new Date();
    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'bock n bisschen zu chatten?? <br/> mir is so langweilig..!! <img src="http://cdn1.cdnthis.com/chatlp/images/facebook-smiley-tongue.png" />' + chatbox_suffix;
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    document.getElementById('msgcount').innerHTML = "";
    setTimeout("waitformsg(1, 'sixt')", randomFromTo(5000, 7000));
}

function sixt() {
    document.getElementById('typing').innerHTML = "";

    var jetzt = new Date();
    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'kennst du schon mein video?<br/>moment^^' + chatbox_suffix;
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    setTimeout("writing('seventh')", randomFromTo(5000, 7000));
}

function seventh() {
    document.getElementById('typing').innerHTML = "";
    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'hier ist mein video:<br/><a target="_blank" href="' + offer_url + '">http://www.youtube.com/wat<br/>ch?v=WfOez0k_H2Y</a>' + chatbox_suffix;
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    var jetzt = new Date();
    setTimeout("writing('eight')", randomFromTo(2000, 5000));
}

function eight() {
    document.getElementById('typing').innerHTML = "";
    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'is lustig oder?<br/>aber dauert<br>nur ein paar sekunden' + chatbox_suffix;
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    var jetzt = new Date();
    setTimeout("writing('ninth')", randomFromTo(2000, 5000));
}

function ninth() {
    document.getElementById('typing').innerHTML = "";
    document.getElementById('chatbox').innerHTML += chatbox_prefix + 'muss weg... <img src="http://cdn1.cdnthis.com/chatlp/images/facebook-smiley-devil.png" /> <img src="http://cdn1.cdnthis.com/chatlp/images/facebook-smiley-devil.png" />' + chatbox_suffix;
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;

    var jetzt = new Date();
    setTimeout("away()", randomFromTo(2000, 5000));
}
function away() {
    document.getElementById('chatbox').innerHTML += '<br><div id="lo" style="color:#aaa;"><i>Kevin Lay ist offline...</i></div>';
    var objDiv = document.getElementById("bb");
    objDiv.scrollTop = objDiv.scrollHeight;
}